﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Itop.KSRY.Model;
using DevExpress.Utils;
using System.Collections;
using System.Threading;

namespace Itop.KSRY {
    public partial class UCksmanKS3 : UserControl
    {
        public UCksmanKS3() {
            InitializeComponent();
            barButtonItem1.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItem1_ItemClick);
            barButtonItem2.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItem2_ItemClick);
        }
        private string[] djNames = new string[] { "公司机关", "一把手", "书记", "副职", "管理人员", "普通工人" };


        void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            //导出
            if(DateTime.Now<new DateTime(2012,5,1))
            export();
        }

        void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            //抽考
           // exam();
            exam1();
        }
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            //colksbz.Visible = false;
            //coljb.Visible = false;
        }

        public void exam() {
            sysset set = DBHelper.SqlMapper.GetOneByKey<sysset>("0");
            if (set == null)
                MessageBox.Show("请先维护抽考参数");
            WaitDialogForm waitdlg =new WaitDialogForm("","开始选取人员,请稍候。。。");
            IList<ksman> list = DBHelper.SqlMapper.GetList<ksman>(null);
            DataTable dt = ConvertHelper.ToDataTable((IList)list, typeof(ksman));
            IList dwlist = DBHelper.SqlMapper.GetList("SelectOneStr", "select distinct dwname from ksman");
            //waitdlg.SetCaption(dwlist.Count + "");
            manlist.Clear();
            int[] djrys = new int[6] ;
            manlist = new List<ksman>();
            string wh1 = "ksbz<>'否' and ";
            if (set.nl > 0)
                wh1 += " nl<" + set.nl + " and ";

            int count=0;
            if (set.dj0 > 0) {
                manlist.AddRange(select(dt.Select(wh1 + "jb=0"), set.dj0));
                djrys[0] = manlist.Count;
                count=manlist.Count;
            }
            if (set.dj1 > 0) {
                manlist.AddRange(select(dt.Select(wh1 + "jb=1"), set.dj1));
                djrys[1] = manlist.Count - count;
                count=manlist.Count;
            }
            if (set.dj2 > 0) {
                //manlist.AddRange(select(dt.Select(wh1 + "jb=2"), set.dj2));
            }
            if (set.dj3 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=3 and dwname='" + dw + "'"), set.dj3));
                djrys[3] = manlist.Count - count;
                count=manlist.Count;

            }
            if (set.dj4 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=4 and dwname='" + dw + "'"), set.dj4));
                djrys[4] = manlist.Count - count;
                count=manlist.Count;
            }
            if (set.dj5 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=5 and dwname='" + dw + "'"), set.dj5));
                djrys[5] = manlist.Count - count;
            }
            StringBuilder sb = new StringBuilder();
            int i = 0;
            foreach (string str in djNames) {
                if (str!="书记")
                {
                    sb.Append(str + "[" + djrys[i] + "]人,");
                }
                i++;
            }
            barStaticItem1.Caption = sb.ToString();
            List<ksman> list1 = new List<ksman>();
            List<ksman> list2 = new List<ksman>();
            foreach (ksman man in manlist) {
                if (man.jb == 0)
                    list1.Add(man);
                else
                    list2.Add(man);
            }
            i = 1;
            foreach (ksman item in list1) {item.orderid = i++;
            }
            i = 1;
            foreach (ksman item in list2) {
                item.orderid = i++;
            }
            this.xtraTabControl1.TabPages.Clear();
            AddPage(list1, list2, "ceshi");
            //ksmanBindingSource.DataSource = list1;
            //ksmanBindingSource2.DataSource = list2;
            //Thread.Sleep(1000);
            waitdlg.Close();
        }

        public void exam1()
        {
            sysset set = DBHelper.SqlMapper.GetOneByKey<sysset>("0");
            if (set == null)
                MessageBox.Show("请先维护抽考参数");
            WaitDialogForm waitdlg = new WaitDialogForm("", "开始选取人员,请稍候。。。");
            IList<ksman> list = DBHelper.SqlMapper.GetList<ksman>(null);
            DataTable dt = ConvertHelper.ToDataTable((IList)list, typeof(ksman));
            IList dwlist = DBHelper.SqlMapper.GetList("SelectOneStr", "select distinct dwname from ksman");
            //waitdlg.SetCaption(dwlist.Count + "");
            listjg.Clear();
            listybs.Clear();
            listsj.Clear();
            listfz.Clear();
            listgy.Clear();
            listpt.Clear();
           listjg = new List<ksman>();
           listybs = new List<ksman>();
           listsj = new List<ksman>();
           listfz = new List<ksman>();
           listgy = new List<ksman>();
            listpt = new List<ksman>();
            manlist.Clear();
            int[] djrys = new int[6];
            manlist = new List<ksman>();
            string wh1 = "ksbz<>'否' and ";
            if (set.nl > 0)
                wh1 += " nl<" + set.nl + " and ";

            int count = 0;
            if (set.dj0 > 0)
            {
                foreach (string dw in dwlist)
                { 
                    listjg.AddRange(select(dt.Select(wh1 + "jb=0 and dwname='" + dw + "'"), set.dj0));
                }
               
                manlist.AddRange(listjg);
                djrys[0] = listjg.Count;
                //count = manlist.Count;
            }
            if (set.dj1 > 0)
            {
                listybs.AddRange(select(dt.Select(wh1 + "jb=1"), set.dj1));
                manlist.AddRange(listybs);
                djrys[1] = listybs.Count;
                //count = manlist.Count;
            }
            if (set.dj2 > 0)
            {
                //manlist.AddRange(select(dt.Select(wh1 + "jb=2"), set.dj2));
            }
            if (set.dj3 > 0)
            {
                //foreach (string dw in dwlist)
                //{
                    listfz.AddRange(select(dt.Select(wh1 + "jb=3"), set.dj3));
                    

               // }
                manlist.AddRange(listfz);
                djrys[3] = listfz.Count;
                //count = manlist.Count;

            }
            if (set.dj4 > 0)
            {
                foreach (string dw in dwlist)
                { 
                listgy.AddRange(select(dt.Select(wh1 + "jb=4 and dwname='" + dw + "'"), set.dj4));
                
                }
                manlist.AddRange(listgy);  
                djrys[4] = listgy.Count;
                //count = manlist.Count;
            }
            if (set.dj5 > 0)
            {
                foreach (string dw in dwlist)
                {
                    listpt.AddRange(select(dt.Select(wh1 + "jb=5 and dwname='" + dw + "'"), set.dj5));
                   
                }
                manlist.AddRange(listpt);
                djrys[5] = listpt.Count;
            }
            StringBuilder sb = new StringBuilder();
            int i = 0;
            foreach (string str in djNames)
            {
                if (str != "书记")
                {
                    sb.Append(str + "[" + djrys[i] + "]人,");
                }
                i++;
            }
            barStaticItem1.Caption = sb.ToString();
            List<ksman> list1 = new List<ksman>();
            List<ksman> list2 = new List<ksman>();
            foreach (ksman man in manlist)
            {
                if (man.jb == 0)
                    list1.Add(man);
                else
                    list2.Add(man);
            }
            i = 1;
            foreach (ksman item in list1)
            {
                item.orderid = i++;
            }
            i = 1;
            foreach (ksman item in list2)
            {
                item.orderid = i++;
            }
            this.xtraTabControl1.TabPages.Clear();
            AddPage(listjg, listybs, "公司机关和一把手");
            AddPage(listfz, listgy, "副职和管理人员");
            AddPage(listpt, listpt, "普通工人");
            //ksmanBindingSource.DataSource = list1;
            //ksmanBindingSource2.DataSource = list2;
            //Thread.Sleep(1000);
            waitdlg.Close();
        }
        int onepagemaxcount = 25;
        private void AddPage(List<ksman> list1,List<ksman> list2,string title )
        {
            if (!title.Contains("普通工人"))
            {
                List<List<ksman>> listcol1 = getpage(list1);
                List<List<ksman>> listcol2 = getpage(list2);
                int maxcount = listcol1.Count > listcol2.Count ? listcol1.Count : listcol2.Count;
                for (int i = 0; i < maxcount; i++)
                {
                    DevExpress.XtraTab.XtraTabPage xtraTabPage1 = new DevExpress.XtraTab.XtraTabPage();
                    if (maxcount > 1)
                        xtraTabPage1.Text = title + "-" + (i+1).ToString();
                    else
                        xtraTabPage1.Text = title;
                    this.xtraTabControl1.TabPages.Add(xtraTabPage1);
                    UCpage page = new UCpage();
                    page.Dock = System.Windows.Forms.DockStyle.Fill;
                    if (listcol1.Count > i)
                    {
                        page.list1 = listcol1[i];
                    }
                    if (listcol2.Count > i)
                    {
                        page.list2 = listcol2[i];
                    }

                    page.init();
                    xtraTabPage1.Controls.Add(page);

                }
            }
            else
            {
                List<List<ksman>> listcol1 = getpage(list1);
                //List<List<ksman>> listcol2 = getpage(list2);
                int maxcount = listcol1.Count ;
                for (int i = 0; i < GetPagecount(maxcount,2); i++)
                {
                    
                   
                    DevExpress.XtraTab.XtraTabPage xtraTabPage1 = new DevExpress.XtraTab.XtraTabPage();
                    if (GetPagecount(maxcount, 2)>1)
                    {
                        xtraTabPage1.Text = title + "-" + (i+1).ToString();
                    }
                    else
                        xtraTabPage1.Text = title;
                    this.xtraTabControl1.TabPages.Add(xtraTabPage1);
                    UCpage page = new UCpage();
                    page.Dock = System.Windows.Forms.DockStyle.Fill;
                   
                    if(2*i<maxcount)
                     page.list1 = listcol1[2*i];

                    if ((2 * i+1) < maxcount)
                    page.list2 = listcol1[2 * i+1];
            
                   
                    //if (listcol1.Count > i)
                    //{
                    //    page.list1 = listcol1[i];
                    //}
                    //if (listcol2.Count > i)
                    //{
                    //    page.list2 = list2;
                    //}

                    page.init();
                    xtraTabPage1.Controls.Add(page);

                }
            }
            
           

        }
        
        private List<List<ksman>> getpage(List<ksman> list1)
        {
            int pageindex = GetPagecount(list1.Count, onepagemaxcount);
            List<List<ksman>> listcollect = new List<List<ksman>>();
            
            for (int j = 1; j <= pageindex; j++)
            {
              
                int prepageindex = j - 1;
                //主题
                int starow = prepageindex * onepagemaxcount + 1;
                int endrow = j * onepagemaxcount;
               if (list1.Count>endrow)
               {
                   List<ksman> list2 = new List<ksman>();
                   for (int i = starow - 1; i < endrow;i++ )
                   {
                       list2.Add(list1[i]);
                   }
                   listcollect.Add(list2);
               }
               else if (list1.Count <= endrow && list1.Count >= starow)
                {
                    List<ksman> list2 = new List<ksman>();
                    for (int i = 0;i< list1.Count - starow + 1;i++ )
                    {
                        list2.Add(list1[starow - 1 + i]);
                    }
                    listcollect.Add(list2);
                }
                


            }
            return listcollect;

        }
        private int GetPagecount(int sum, int rowcount)
        {
            if (rowcount != 0)
            {
                if (sum % rowcount != 0)
                {
                    return (sum / rowcount + 1);
                }
                else
                {
                    return sum / rowcount;
                }
            }
            else
                return 1;

        }
        List<ksman> manlist = new List<ksman>();
        List<ksman> listjg = new List<ksman>();
        List<ksman> listybs = new List<ksman>();
        List<ksman> listsj = new List<ksman>();
        List<ksman> listfz = new List<ksman>();
        List<ksman> listgy = new List<ksman>();
        List<ksman> listpt = new List<ksman>();

        private IList<ksman> select(DataRow[] rows, int num) {
            IList<ksman> list = new List<ksman>();
            if (num >= rows.Length) {
                foreach (DataRow row in rows) {
                    list.Add(ConvertHelper.RowToObject<ksman>(row));
                }
            } else {
                ArrayList rowlist = new ArrayList(rows);
                int listnum = rowlist.Count;
                Random random=new Random();
                while (num > 0) {
                    DataRow row = rowlist[random.Next(1,listnum--) - 1] as DataRow;
                    rowlist.Remove(row);
                    num--;
                    list.Add(ConvertHelper.RowToObject<ksman>(row));
                }
            }
            return list;
        }
        private void export() {
            if (manlist.Count == 0) return;
            WaitDialogForm waitdlg = new WaitDialogForm("", "正在导出数据,请稍候。。。");
            DBHelper.ExportExcel(manlist);
            waitdlg.Close();
        }
    }
}
